package com.cqyanyu.yanyu.http;

import android.content.Context;

import com.cqyanyu.yanyu.utils.XNetworkUtil;
import com.cqyanyu.yanyu.utils.XToastUtil;

import org.xutils.common.Callback;
import org.xutils.ex.HttpException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.net.UnknownHostException;

/**
 * Created by cheng on 2016/6/24.
 */
public class XApi {
    public static final Callback.Cancelable get(final Context context, RequestParams requestParams, final Callback.CommonCallback callback) {
        if (callback instanceof Callback.CacheCallback) {
            return x.http().get(requestParams, getCacheCallback(context, (Callback.CacheCallback) callback));
        } else if (callback instanceof Callback.CommonCallback) {
            return x.http().get(requestParams, getCommonCallback(context, callback));
        } else {
            callback.onFinished();
            return null;
        }
    }

    public static final Callback.Cancelable post(final Context context, RequestParams requestParams, final Callback.CommonCallback<String> callback) {
        return x.http().post(requestParams, getCommonCallback(context, callback));
    }

    private static final Callback.CacheCallback getCacheCallback(final Context context, final Callback.CacheCallback<String> callback) {
        return new Callback.CacheCallback<String>() {
            @Override
            public boolean onCache(String result) {
                return callback.onCache(result);
            }

            @Override
            public void onSuccess(String result) {
                XResultNoData xResultNoData = new XResultNoData(result);
                if (xResultNoData.code != 5) {
                    callback.onSuccess(result);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (ex instanceof UnknownHostException) {
                    XNetworkUtil.setNetworkMethod(context);
                } else if (ex instanceof HttpException) { // 网络错误
//                    HttpException httpEx = (HttpException) ex;
//                    int responseCode = httpEx.getCode();
//                    String responseMsg = httpEx.getMessage();
//                    String errorResult = httpEx.getResult();
                    XToastUtil.showToast(context, "连接服务器超时");
                    // ...
                } else {
                    callback.onError(ex, isOnCallback);
                }

            }

            @Override
            public void onCancelled(CancelledException cex) {
                callback.onCancelled(cex);
            }

            @Override
            public void onFinished() {
                callback.onFinished();

            }
        };
    }

    private static final Callback.CommonCallback getCommonCallback(final Context context, final Callback.CommonCallback<String> callback) {
        return new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                XResultNoData xResultNoData = new XResultNoData(result);
                if (xResultNoData.code != 5) {
                    callback.onSuccess(result);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (ex instanceof UnknownHostException) {
                    XNetworkUtil.setNetworkMethod(context);
                } else if (ex instanceof HttpException) { // 网络错误
//                    HttpException httpEx = (HttpException) ex;
//                    int responseCode = httpEx.getCode();
//                    String responseMsg = httpEx.getMessage();
//                    String errorResult = httpEx.getResult();
                    XToastUtil.showToast(context, "连接服务器超时");
                    // ...
                } else {
                    callback.onError(ex, isOnCallback);
                }

            }

            @Override
            public void onCancelled(CancelledException cex) {
                callback.onCancelled(cex);
            }

            @Override
            public void onFinished() {
                callback.onFinished();
            }
        };
    }
}
